package com.colorful.insuranceproject.utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.listener.OnDismissListener;
import com.colorful.insuranceproject.R;
import com.lovely3x.common.activities.BaseCommonActivity;
import com.lovely3x.common.utils.Displayable;
import com.colorful.insuranceproject.utils.wheel.OptionsPickerView;
import com.colorful.insuranceproject.utils.wheel.WheelOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class WheelActivity extends BaseCommonActivity {

    /**
     * 需要显示的数据
     */
    public static final String EXTRA_DATA = "extra.data";

    public static final String EXTRA_RESULT_PARENT = "extra.result.parent";
    public static final String EXTRA_RESULT_MIDDLE = "extra.result.middle";
    public static final String EXTRA_RESULT_CHILD = "extra.result.child";

    @Bind(R.id.fl_activity_wheel_container)
    ViewGroup vgContainer;

    private ArrayList<Parent> mColumnDatas;
    private OptionsPickerView pvOptions;

    public static void launchMeForResult(Activity activity, List<? extends Parent> data, int requestCode) {
        Intent intent = makeIntent(activity, data);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void launchMe(Activity activity, ArrayList<? extends Parent> data) {
        Intent intent = makeIntent(activity, data);
        activity.startActivity(intent);
    }

    @NonNull
    public static Intent makeIntent(Activity activity, List<? extends Parent> data) {
        Intent intent = new Intent(activity, WheelActivity.class);
        intent.putParcelableArrayListExtra(EXTRA_DATA, data instanceof ArrayList ? (ArrayList<? extends Parcelable>) data : new ArrayList<Parcelable>(data));
        return intent;
    }

    public static <T extends Parcelable> T getParentData(Intent intent) {
        return intent.getParcelableExtra(EXTRA_RESULT_PARENT);
    }

    public static <T extends Parcelable> T getMiddleData(Intent intent) {
        return intent.getParcelableExtra(EXTRA_RESULT_MIDDLE);
    }

    public static <T extends Parcelable> T getChildData(Intent intent) {
        return intent.getParcelableExtra(EXTRA_RESULT_CHILD);
    }


    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        this.mColumnDatas = bundle.getParcelableArrayList(EXTRA_DATA);
        if (mColumnDatas == null) throw new IllegalArgumentException();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_wheel;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onViewInitialized() {

        pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                WheelOptions options = pvOptions.getWheelOptions();

                //设置结果给调用者
                Intent intent = new Intent();
                if (options.getParents() != null && !options.getParents().isEmpty()) {
                    Parent parent = (Parent) options.getParents().get(options1);
                    intent.putExtra(EXTRA_RESULT_PARENT, parent);

                    if (parent.getMiddles() != null && parent.getMiddles().size() > option2) {
                        Middle middle = parent.getMiddles().get(option2);
                        intent.putExtra(EXTRA_RESULT_MIDDLE, middle);

                        if (middle.getChildren() != null && middle.getChildren().size() > options3) {
                            intent.putExtra(EXTRA_RESULT_CHILD, middle.getChildren().get(options3));
                        }
                    }
                }
                setResult(RESULT_OK, intent);
                pvOptions.dismiss();
            }

            @Override
            public void onCancelClicked() {
                setResult(RESULT_CANCELED);
                pvOptions.dismiss();
            }
        }).setDividerType(WheelView.DividerType.FILL)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0, 0, 0)  //设置默认选中项
                .isDialog(false)//设置为对话框模式
                .setOutSideCancelable(false).setCyclic(false, false, false)
                .build();

        pvOptions.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(Object o) {
                finish();
            }
        });

        pvOptions.setPicker(mColumnDatas);

        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getWindow().getDecorView().getViewTreeObserver().removeGlobalOnLayoutListener(this);
                pvOptions.show();
            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 适配器处理
     */
    public static class Parent implements Parcelable {

        private List<Middle> mMiddles;

        private Displayable mData;

        public Parent() {
        }

        public Parent(Displayable data) {
            mData = data;
        }

        public Parent(Displayable data, List<Middle> middles) {
            mMiddles = middles;
            mData = data;
        }

        public List<Middle> getMiddles() {
            return mMiddles;
        }

        public void setMiddles(List<Middle> middles) {
            mMiddles = middles;
        }


        @Override
        public String toString() {
            return mData == null ? null : mData.display();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeTypedList(this.mMiddles);
            dest.writeParcelable(this.mData, flags);
        }

        protected Parent(Parcel in) {
            this.mMiddles = in.createTypedArrayList(Middle.CREATOR);
            this.mData = in.readParcelable(Displayable.class.getClassLoader());
        }

    }

    public static class Middle implements Parcelable {

        private List<Child> mChildren;

        private Displayable mData;

        private String id;


        public Middle() {
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Middle{" +
                    "mChildren=" + mChildren +
                    ", mData=" + mData +
                    ", id='" + id + '\'' +
                    '}';
        }



        public List<Child> getChildren() {
            return mChildren;
        }

        public void setChildren(List<Child> children) {
            mChildren = children;
        }


        public Middle(Displayable data, List<Child> children) {
            mChildren = children;
            mData = data;
        }

        public Middle(Displayable data) {
            mData = data;
        }



        public Displayable getData() {
            return mData;
        }

        public void setData(Displayable data) {
            mData = data;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeTypedList(this.mChildren);
            dest.writeParcelable(this.mData, flags);
            dest.writeString(this.id);
        }

        protected Middle(Parcel in) {
            this.mChildren = in.createTypedArrayList(Child.CREATOR);
            this.mData = in.readParcelable(Displayable.class.getClassLoader());
            this.id = in.readString();
        }

        public static final Creator<Middle> CREATOR = new Creator<Middle>() {
            @Override
            public Middle createFromParcel(Parcel source) {
                return new Middle(source);
            }

            @Override
            public Middle[] newArray(int size) {
                return new Middle[size];
            }
        };
    }

    public static class Child implements Parcelable {

        private Displayable mData;

        private String id;

        public Child() {
        }

        public Child(Displayable mData, String id) {
            this.mData = mData;
            this.id = id;
        }

        public Displayable getmData() {
            return mData;
        }

        public void setmData(Displayable mData) {
            this.mData = mData;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Child{" +
                    "mData=" + mData +
                    ", id='" + id + '\'' +
                    '}';
        }


        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeParcelable(this.mData, flags);
            dest.writeString(this.id);
        }

        protected Child(Parcel in) {
            this.mData = in.readParcelable(Displayable.class.getClassLoader());
            this.id = in.readString();
        }

        public static final Creator<Child> CREATOR = new Creator<Child>() {
            @Override
            public Child createFromParcel(Parcel source) {
                return new Child(source);
            }

            @Override
            public Child[] newArray(int size) {
                return new Child[size];
            }
        };
    }
}
